<?php
/**
 * @Copyright   2008, XXXX-XXXX
 * All rights reserved. 
 * feature summary.
 * @filesource  index.php 
 * @author      lipeng <lipengage@gmain.com> 
 * @version     $Id: $ 
 * @package     application
 * @subpackage  
 */

require 'config.php';

function __autoload($class_name) {
	$locations = array(
		'common',
		'converters',
		'securitytoken',
		'service',
		'servlet',
	);

	// Import extension class paths
	$extension_class_paths = Config::app_get('extension_class_paths');
	if (is_array($extension_class_paths)) {
		$locations = array_merge($locations, $extension_class_paths);
	}
	else if(!empty($extension_class_paths)) {
		$locations = array_merge(explode(',', $extension_class_paths), $locations);
	}

	// Check for the presense of this class in our all our directories.
	$file_name = $class_name . '.php';
	foreach ($locations as $path) {
		if (file_exists("{$path}/{$file_name}")) {
			require "{$path}/{$file_name}";
			break;
		}
	}
}

$servletMap = array(
	Config::get('web_prefix').'/data' => 'data_servlet',
	Config::get('web_prefix').'/js' => 'js_servlet',
);

// Try to match the request url to our servlet mapping
$servlet = false;

$uri = $_SERVER["REQUEST_URI"];
foreach ($servletMap as $url => $class) {
	if (substr($uri, 0, strlen($url)) == $url) {
		//Fixme temporary hack to support both /data and /js with the same event handler
		$servlet = $class;
		break;
	}
}

if ($servlet) {
	$class = new $class();
	$method = $_SERVER['REQUEST_METHOD'];
	// Not all clients support the PUT, HEAD & DELETE http methods, they depend on the X-HTTP-Method-Override instead 
	if ($method == 'POST' && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
		$method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
	}
	$method = 'do_' . strtolower($method);
	if (is_callable(array($class, $method))) {
		$class->$method();
	}
}
?>